package io.itit.ecp.admin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.common.enums.IActivityCarbonEmissionDataTypeEnums;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.mapper.*;
import io.itit.ecp.admin.server.service.IActivityCarbonEmissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;


@Service
@RequiredArgsConstructor
public class ActivityCarbonEmissionServiceImpl extends JoinServiceImpl<ActivityCarbonEmissionMapper, ActivityCarbonEmissionEntity>
        implements IActivityCarbonEmissionService {

    private final ActivityCarbonEmissionDataMapper activityCarbonEmissionDataMapper;

    private final MeterDeviceMapper meterDeviceMapper;

    private final CarbonSinkMapper carbonSinkMapper;

    @Override
    public ActivityCarbonEmissionEntity findFirstByActivityName(String name) {
        List<ActivityCarbonEmissionEntity> emissionEntities = this.lambdaQuery()
                .eq(ActivityCarbonEmissionEntity::getActivityName, name)
                .list();
        return CollectionUtils.isEmpty(emissionEntities) ? null : emissionEntities.get(0);
    }

    @Override
    @Transactional
    public boolean saveOrUpdateActivityCarbonEmission(ActivityCarbonEmissionEntity activityCarbonEmissionEntity,
                                                      List<ActivityCarbonEmissionDataEntity> dataEntities) {
        boolean isUpdate = StringUtils.hasLength(activityCarbonEmissionEntity.getId());
        boolean result = this.saveOrUpdate(activityCarbonEmissionEntity);
        if (isUpdate) {
            activityCarbonEmissionDataMapper.delete(new LambdaQueryWrapper<ActivityCarbonEmissionDataEntity>()
                    .eq(ActivityCarbonEmissionDataEntity::getActivityCarbonId, activityCarbonEmissionEntity.getId()));
        }
        if (!CollectionUtils.isEmpty(dataEntities)) {
            for (ActivityCarbonEmissionDataEntity item : dataEntities) {
                String dataType = item.getDataType();
                String sceneEdificeId = null;
                String sceneFloorId = null;
                String sceneRoomId = null;
                if (IActivityCarbonEmissionDataTypeEnums.RELATED_MEASURING_EQUIPMENT.getValue().equals(dataType)) {
                    MeterDeviceEntity meterDeviceEntity = meterDeviceMapper.selectById(item.getTargetId());
                    if (Objects.nonNull(meterDeviceEntity)) {
                        sceneEdificeId = meterDeviceEntity.getSceneEdificeId();
                        sceneFloorId = meterDeviceEntity.getSceneFloorId();
                        sceneRoomId = meterDeviceEntity.getSceneRoomId();
                    }
                } else if (IActivityCarbonEmissionDataTypeEnums.RELATED_GREENING.getValue().equals(dataType)) {
                    CarbonSinkEntity carbonSinkEntity = carbonSinkMapper.selectById(item.getTargetId());
                    if (Objects.nonNull(carbonSinkEntity)) {
                        sceneEdificeId = carbonSinkEntity.getSceneEdificeId();
                        sceneFloorId = carbonSinkEntity.getSceneFloorId();
                        sceneRoomId = carbonSinkEntity.getSceneRoomId();
                    }
                }
                item.setSceneEdificeId(sceneEdificeId);
                item.setSceneFloorId(sceneFloorId);
                item.setSceneRoomId(sceneRoomId);
                item.setActivityCarbonId(activityCarbonEmissionEntity.getId());
                activityCarbonEmissionDataMapper.insert(item);
            }
        }
        return result;
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        activityCarbonEmissionDataMapper.delete(new LambdaQueryWrapper<ActivityCarbonEmissionDataEntity>()
                .eq(ActivityCarbonEmissionDataEntity::getActivityCarbonId, id));
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean removeBatchByIds(Collection<?> ids, int batchSize) {
        activityCarbonEmissionDataMapper.delete(new LambdaQueryWrapper<ActivityCarbonEmissionDataEntity>()
                .in(ActivityCarbonEmissionDataEntity::getActivityCarbonId, ids));
        return super.removeBatchByIds(ids, batchSize);
    }
}
